const pool = require('./database');
const fs = require('fs');
const path = require('path');
const bcrypt = require('bcryptjs');

async function initDatabase() {
  try {
    // 读取SQL文件
    const sqlPath = path.join(__dirname, 'init.sql');
    const sql = fs.readFileSync(sqlPath, 'utf8');

    // 执行SQL语句
    await pool.query(sql);

    // 创建默认管理员用户
    const hashedPassword = await bcrypt.hash('admin123', 10);
    await pool.query(
      'INSERT INTO users (username, password, role) VALUES (?, ?, ?)',
      ['admin', hashedPassword, 'admin']
    );

    console.log('数据库初始化成功');
  } catch (error) {
    console.error('数据库初始化失败:', error);
  } finally {
    await pool.end();
  }
}

initDatabase(); 